﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NewPRProcess.aspx.cs" Inherits="PRProcess.NewPRProcess" %>

<%@ Register Src="../../Processes/ProcessControl/ButtonList.ascx" TagName="buttonlist"
    TagPrefix="btn" %>
<%@ Register Src="../../Processes/ProcessControl/ApprovalHistory.ascx" TagName="approvalhistory"
    TagPrefix="ah" %>
<%@ Register Src="../../Processes/ProcessControl/Attachments.ascx" TagName="attachments"
    TagPrefix="attach" %>
<%@ Register Src="../../Processes/ProcessControl/UserInfo.ascx" TagName="userinfo"
    TagPrefix="ui" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="NewPRProcessHead" runat="server">
    <title>Purchasing Requisition采购申请</title>
    <script src="../../js/jquery.js" type="text/javascript"></script>
    <script src="../../js/common.js" type="text/javascript"></script>
    <script src="../../js/jquery.validationEngine.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../js/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript">
        setInterval("autoComputer()", 200);



        function autoComputer() {
            var totalAmount = 0.00;
            $("#tbDetail tr:gt(0)").each(function (i, tr) {
                var qty = parseFloat($(tr).find("td:eq(7) :first-child").val());
                var price = parseFloat($(tr).find("td:eq(8) :first-child").val());
                //var subTotal = parseFloat(qty * price).toFixed(2);
                var subTotal = parseFloat(qty * price);
                if (subTotal) {
                    $(tr).find("td:eq(9) :first-child").val(subTotal);
                    totalAmount = (totalAmount + subTotal).toFixed(2);
                    $("#fld_TOTALAMOUNT").val(totalAmount);
                }
            });          
        }
        //选择弹出页面
        function selectPage1() {
            var dpt = $("[id$=fld_DEPARTMENT]").text();
            // alert(dpt);
            rtn = selectPage("select C.DOCUMENTNO,C.PROJECTNUMBER,C.APPLICANT,C.REQUESTDATE,C.ITCAPEX,ISNULL(C.PROJECTLEADERAD,''),ISNULL(C.PROJECTLEADER,''),C.PROJECTLEADER, C.PROJECTLEADERAD from dbo.PROC_CAPEX  as C inner join dbo.PROC_CAPEX_DT as P on p.FORMID=C.FORMID  where C.DEPARTMENT='" + dpt + "' and  C.STATUS=2  and P.STATUS=0", "REQUESTDATE", "DOCUMENTNO,PROJECTNUMBER,APPLICANT,REQUESTDATE,ITCAPEX,PROJECTLEADER", "Capex No./单号,Project No./项目编号,Applicant/申请人,Request Date/申请时间", "100,60,80,80,0", "选择Capex");
            if (rtn) {
                $("#fld_CERNO").val(rtn.DOCUMENTNO);
                $("#fld_PRODUCTLINE").val(rtn.PROJECTNUMBER);
                $("#var_isItCapex").val(rtn.ITCAPEX);
                $("#var_PROJECTLEADERAD").val($.trim(rtn.PROJECTLEADERAD));
            }
            else {
                $("#fld_CERNO").val("");
                $("#fld_PRODUCTLINE").val("");
                $("#var_isItCapex").val("");
                $("#var_PROJECTLEADERAD").val("");

            }
            return false;
        }

        function selectBudgetInfo() {
            var val = $(this).parent().parent().find("[id$=CAPEXITEM]").val();
            //alert(val);
            if (val) {
                return false;
            }
            else {
                var dpt = $("[id$=fld_DEPARTMENT]").text();
                // alert(dpt);
                rtn = selectPage("select BUDGETCODE,ITEMSDESC,AMOUNT,PRBALANCE,AMOUNT-PRBALANCE BALANCE from PROC_BUDGETSET where department ='" + dpt + "' and  BUDGETTYPE ='E' ", "BUDGETCODE", "BUDGETCODE,ITEMSDESC,AMOUNT,BALANCE,PRBALANCE", "Budget Code/预算编号,Budget Desc./预算描述,Amount/金额,Use Balance/已用余额,PR Balance/PR余额", "80,120,50,60,70", "选择费用类的预算信息");
                if (rtn) {
                    $(this).parent().parent().find("[id$=fld_ITEMNUMBER]").val(rtn.BUDGETCODE);
                    $(this).parent().parent().find("[id$=fld_BALANCE]").val(rtn.BALANCE);
                }

            }
            //alert($(this).val());
            ValidateRowData(this);
        };

        function CheckSelect(ID) {
            $("#" + ID).parent().parent().find("[id$=fld_COSTCATEGORY]").val($("#" + ID + "").val());
        }

        function CheckSelectSub(ID) {
            $("#" + ID).parent().parent().find("[id$=fld_SUBCATAGARY]").val($("#" + ID + "").val());
        }

        //预算编号、判断品名、余额为空的话，就属于预算外的申请，该申请需要提交至GM审批
        //另外判断，如果本次申请的金额大于余额的话，也需要到GM审批
        function ValidateRowData(ert) {
            var ItemName = $(ert).parent().parent().find("[id*=fld_ITEMDECRIPTION]").val();
            var BudgetNumber = $(ert).parent().parent().find("[id*=fld_ITEMNUMBER]").val();
            var Amount = $(ert).parent().parent().find("[id*=fld_ESTIMATEDPRICE]").val();
            var BalanceValue = $(ert).parent().parent().find("[id*=fld_BALANCE]").val();
            var Balance = Math.round(BalanceValue);   //将小数转换为整数      
            var NoBudget = "1";  //预算外
            var Budget = "0";    //预算内    
            var Qty = $(ert).parent().parent().find("[id*=fld_QUANTITY]").val();   // 数量
            $(ert).parent().parent().find("[id*=fld_USEDQUANTITY]").val(Qty);     //设置已用数量的初始值为当前输入的数量
            if (ItemName == "" && BudgetNumber == "" && Balance == "") {
                $("#fld_NOBUDGETED").val(NoBudget);
                //alert("111");               
            }
            else if (Amount > Balance) {
                $("#fld_NOBUDGETED").val(NoBudget);
                //alert("222");  
            }
            else {
                $("#fld_NOBUDGETED").val("0");
                $("#fld_BUDGETED").val(Budget);
                //alert("000");           
            }                 
        }

        $().ready(function () {
            $("#tbDetail :input[type!=button][type!=submit]").each(function () {
                $(this).attr("AutoWidth", "true");
            });

            //            $("#tbDetail select").each(function () {
            //                alert($(this).attr("AutoWidth"));
            //            });

            // $("#tbDetail ")
            $("input[id$=fld_ITEMNUMBER]").each(function () {
                $(this).click(selectBudgetInfo);
            });

            $("[id*=fld_SUBCATAGARY]").val("");  //页面再一次刷新时，清空fld_SUBCATAGARY文本框的值。

            $(":input[AutoWidth=true]").each(function (i) {
                //alert($.type($(this)));
                var oWidth = $(this).parent().width();
                $(this).bind("focus", function () {
                    //alert($(this).parent().width(200));
                    $(this).parent().width(200);
                });
                $(this).bind("blur", function () {
                    //alert($(this).parent().width(200));
                    $(this).parent().width(oWidth);
                });
            })
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="container">
        <div class="row">
            <ui:userinfo id="UserInfo1" processtitle="Purchasing Requisition<br/>采购申请" processprefix="PR"
                tablename="PROC_PURCHASEREQUEST" tablenamedetail="PROC_PURCHASEREQUEST_DT" runat="server"></ui:userinfo>
        </div>
        <div class="row">
            <table class="table table-condensed table-bordered">
                <tr>
                    <td class="banner" colspan="6">
                        Application Details / 详细信息
                    </td>
                </tr>
                <tr>
                    <td class="td-label">
                        if CER,No.:/若是资本支出,号码
                    </td>
                    <td class="td-content">
                        <asp:TextBox ID="fld_CERNO" runat="server" disInput="true" Enabled="true" ToolTip="请单击...选择！"></asp:TextBox>
                        <asp:Button ID="Button1" runat="server" Text="..." Width="10%" Height="25px" CssClass="btn"
                            OnClientClick="return selectPage1();" />
                    </td>
                    <td class="td-label">
                        Project/Product Line/项目/产品类<span class="red">*</span>
                    </td>
                    <td class="td-content">
                        <asp:TextBox ID="fld_PRODUCTLINE" runat="server" CssClass="validate[required]"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="td-label">
                        Applicant Phone/申请人电话
                    </td>
                    <td class="td-content">
                        <asp:TextBox ID="fld_APPLICANTPHONE" runat="server"></asp:TextBox>
                    </td>
                    <td class="td-label">
                        Recommended Supplier/<br />
                        推荐供应商
                    </td>
                    <td class="td-content" colspan="3">
                        <asp:TextBox ID="fld_RECOMMENDSUPPLIER" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="td-label">
                        Category/大类<span class="red">*</span>
                    </td>
                    <%-- <td class="td-content"  colspan="3">
                            <asp:CheckBox ID="fld_ISTOOLS" runat="server"/>
                            Tools/工具
                               <span style=" padding-left:140px">
                            <asp:CheckBox ID="fld_ISPARTS" runat="server"  TextAlign="Left" /> 
                            Spare Parts/备品备件
                            </span>
                             <span style=" padding-left:140px">
                            <asp:CheckBox ID="fld_ISCONSUMABLES" runat="server"  TextAlign="Left" /> 
                            Consumables/消耗材料
                            </span>
                        </td>--%>
                    <td class="td-content" colspan="4">
                        <asp:DropDownList ID="CATAGARY" runat="server" CssClass="validate[required]" DataTextField="CNNAME"
                            DataValueField="RESOURCEID" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="fld_CATAGARY_SelectedIndexChanged"
                            OnPreRender="CATAGARY_PreRender">
                            <asp:ListItem Text="---请选择大类---" Value=""></asp:ListItem>
                        </asp:DropDownList>
                        <asp:TextBox ID="fld_CATAGARY" runat="server" Style="display: none;"></asp:TextBox>
                    </td>
                   <%-- <td class="td-label">
                        Subcategories/小类<span class="red">*</span>
                    </td>
                    <td class="td-content">
                        <asp:DropDownList ID="fld_SUBCATAGARY" CssClass="validate[required]" runat="server"
                            AppendDataBoundItems="true" DataTextField="CNNAME">
                            <asp:ListItem Text="---请选择大类,再选择小类---" Value="" Selected="True"></asp:ListItem>
                        </asp:DropDownList>
                    </td>--%>
                    <%-- <td class="td-content"  colspan="3">
                            <asp:CheckBox ID="fld_ISMACHINE" runat="server"  TextAlign="Left" /> 
                            Machine & Equipment/机器设备
                            <span style=" padding-left:28px">
                            <asp:CheckBox ID="fld_ISFIXTURE" runat="server"  TextAlign="Left" /> 
                            Fixture & Furtuniture/工装,夹具及家具    </span> 
                            <br />                    
                            <asp:CheckBox ID="fld_ISARICLES" runat="server"  TextAlign="Left" /> 
                            Low Value Aricles 低值易耗件
                             <span style=" padding-left:39px">
                            <asp:CheckBox ID="fld_ISLICENSE" runat="server"  TextAlign="Left" /> 
                            Service/License/服务/许可证
                             </span>
                        </td>--%>
                </tr>
            </table>
        </div>
        <div>
            <tr>
                <td colspan="6">
                    <table class="table table-condensed table-bordered tablerequired" id="tbDetail">
                        <tr>
                            <th style="display: none">
                                No<br />
                                序号
                            </th>
                            <th style="width: 90px">
                                Capex Item<br />
                                Capex品名
                            </th>
                            <th>
                                Budget Number<br />
                                预算编号<span class="red">*</span>
                            </th>
                            <th>
                                Item Description<br />
                                品号 / 品名<span class="red">*</span>
                            </th>
                            <th>
                                Cost Category<br />成本类别 <span class="red">*</span>
                            </th>
                            <th>
                                Sub<br />Categories小类<span class="red">*</span>
                            </th>
                            <th>
                                Spec<br />
                                规格
                            </th>
                            <th>
                                Quantity<br />
                                数量<span class="red">*</span>
                            </th>
                            <th>
                                Unit Price<br />
                                单价<span class="red">*</span>
                            </th>
                            <th>
                                Estimated Price<br />
                                估价 (RMB)
                            </th>
                            <th>
                                Expected Delivery<br />
                                期望到货日期
                            </th>
                            <th>
                                Owner/Phone<br />
                                负责人/电话
                            </th>
                            <th>
                                Balance
                                <br />
                                余额
                            </th>
                            <th>
                                <asp:Button ID="btnAdd" runat="server" Text="Add/增加" CssClass="btn" CausesValidation="false"
                                    OnClick="btnAdd_Click" />
                            </th>
                        </tr>
                        <asp:Repeater ID="fld_detail_PROC_PURCHASEREQUEST_DT" runat="server" OnItemCommand="fld_detail_PROC_PURCHASEREQUEST_DT_ItemCommand"
                            OnItemDataBound="fld_detail_PROC_PURCHASEREQUEST_DT_ItemDataBound" OnPreRender="fld_detail_PROC_PURCHASEREQUEST_DT_PreRender">
                            <ItemTemplate>
                                <tr>
                                    <td style="display: none;">
                                        <asp:TextBox ID="fld_ID" Text='<%# Container.ItemIndex+1 %>' runat="server" Width="80%"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_FORMID" Text='<%#Eval("FORMID") %>' runat="server" Style="display: none"></asp:TextBox>
                                        <asp:DropDownList ID="CAPEXITEM" runat="server" Width="95%" OnSelectedIndexChanged="fld_CAPEX_SelectedIndexChanged"
                                            AutoPostBack="true">
                                        </asp:DropDownList>
                                        <asp:TextBox ID="fld_CAPEXITEM" Text='<%# Eval("CAPEXITEM") %>' runat="server"  Style="display: none"></asp:TextBox>
                                    </td>
                                    <%--  <td>
                                                <asp:TextBox ID="fld_PHONE"  Text=''  runat="server" Width="85%" CssClass="validate[required]" style=" cursor:hand" onclick="return selectPage1();"></asp:TextBox>
                                            </td>--%>
                                    <td>
                                        <asp:TextBox ID="fld_ITEMNUMBER" disInput="true" Text='<%# Eval("ITEMNUMBER") %>'
                                            runat="server" Width="80%" ToolTip="请选择Capex品名自动带出或单击选择" CssClass="validate[required]"></asp:TextBox>
                                    </td>                                    
                                    <td>
                                        <asp:TextBox ID="fld_ITEMDECRIPTION" Text='<%# Eval("ITEMDECRIPTION") %>' runat="server"
                                            Width="85%" CssClass="validate[required]"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="COSTCATEGORY" runat="server" Width="85%" CssClass="validate[required]" onchange="CheckSelect(this.id);">
                                          <asp:ListItem Selected="True" Value="">--请选择成本类别信息--</asp:ListItem>
                                          <asp:ListItem  Value="Repeatable" Text="Repeatable">Repeatable</asp:ListItem>
                                          <asp:ListItem  Value="Discrete" Text="Discrete">Discrete</asp:ListItem>
                                        </asp:DropDownList>
                                        <asp:TextBox ID="fld_COSTCATEGORY" Text='<%# Eval("COSTCATEGORY") %>' runat="server"  Style="display: none"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="SUBCATAGARY" runat="server" Width="85%" CssClass="validate[required]" AppendDataBoundItems="true" DataTextField="CNNAME" onchange="CheckSelectSub(this.id);">                                          
                                             <asp:ListItem Text="---请选择小类---" Value="" Selected="True"></asp:ListItem>
                                        </asp:DropDownList>
                                         <asp:TextBox ID="fld_SUBCATAGARY" Text='<%# Eval("SUBCATAGARY") %>' runat="server"   Style="display: none"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_SPEC" Text='<%# Eval("SPEC") %>' runat="server" Width="85%"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_QUANTITY" Text='<%# Eval("QUANTITY") %>' money='money' runat="server" onblur="ValidateRowData(this)" onkeyup="ValidateRowData(this)"
                                            Width="85%" CssClass="validate[required]"></asp:TextBox>
                                        <asp:TextBox ID="fld_USEDQUANTITY" Text='<%# Eval("QUANTITY") %>'  money='money' runat="server"   Style="display: none"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_UNITPRICE" Text='<%# Eval("UNITPRICE") %>' money="money" runat="server"  onblur="ValidateRowData(this)" onkeyup="ValidateRowData(this)"
                                            Width="85%"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_ESTIMATEDPRICE" Text='<%# Eval("ESTIMATEDPRICE") %>' disInput="true"  
                                            money="money" runat="server" Width="85%"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_EXPECTDELIVERY" onFocus='WdatePicker()' Width="80%" Text='<%# Eval("EXPECTDELIVERY") %>'
                                            runat="server"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_OWNER" Text='<%# Eval("OWNER") %>' runat="server" Width="85%"
                                            CssClass="validate[required]"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="fld_BALANCE" disInput="true" Text='<%# Eval("BALANCE") %>' runat="server"
                                            Width="80%"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:Button ID="btnDelete" runat="server" Text="Delete/删除" CssClass="btn" CommandName="del"
                                            ClientIDMode="Static" OnClientClick="return confirm('Confirm Delete/确认删除？')" />
                                    </td>
                                    
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>
                    </table>
                </td>
            </tr>
        </div>
        <div class="row">
            <attach:attachments id="Attachments1" runat="server"></attach:attachments>
        </div>
        <div class="row">
            <ah:approvalhistory id="ApprovalHistory1" showaction="false" runat="server"></ah:approvalhistory>
        </div>
        <div class="row">
            <btn:buttonlist id="ButtonList1" runat="server"></btn:buttonlist>
        </div>
    </div>
    <div style="display: none;">
        <asp:TextBox ID="var_isItCapex" runat="server"></asp:TextBox>
        <asp:TextBox ID="var_PROJECTLEADERAD" runat="server"></asp:TextBox>
        <asp:TextBox ID="fld_TOTALAMOUNT" runat="server"></asp:TextBox>
        <asp:HiddenField ID="hfselectCatagory" runat="server" />
       
    </div>   
        <asp:TextBox ID="fld_BUDGETED" runat="server"></asp:TextBox>   <%--预算内--%>
        <asp:TextBox ID="fld_NOBUDGETED" runat="server"></asp:TextBox> <%--预算外--%>
        <asp:TextBox ID="fld_ISOPERATION" runat="server"></asp:TextBox> <%--是否为运营部门，1代表是，0代表否--%>      
    </form>
</body>
</html>
